package com.iris.live.services.data.repositories.impl;

import com.iris.live.services.common.StringUtils;
import com.iris.live.services.common.reconsitution.FinanceConstant;
import com.iris.live.services.data.repositories.FinanceReturnTicketRepository;
import com.iris.live.services.models.PageData;
import com.iris.live.services.services.FinanceReturnTicketService;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 财务系统对接模块(退票模块)--持久层
 */
@Repository
public class FinanceReturnTicketRepositoryImpl extends BaseRepository implements FinanceReturnTicketRepository{

    @Override
    public PageData getReturnTicketForPage(FinanceReturnTicketService.ReturnTicketPageRequest request) {
        List<String> typeList = new ArrayList<>();
        typeList.add(FinanceConstant.FAPIAO);  // 需求：列表中只显示发票
        String sql = "select * \n" +
                "from( \n" +
                "select \n" +
                "  h.报价编号,\n" +
                "  a.财务编号,\n" +
                " b.账目编号,\n" +
                "  h.初始报价单号 订单号,\n" +
                "  case\n" +
                " when g.订单状态 = '意向订单C' and not isnull(g.订单审批通过日期) and date(g.订单审批通过日期) >= ifnull(g.订单失销日期,'1970-01-01') and g.订金类型 = '意向金'  then '意向订单'\n" +
                " when g.订单状态 = '订单C' and not isnull(g.订单审批通过日期) and date(g.订单审批通过日期) >= ifnull(g.订单失销日期,'1970-01-01') and g.订金类型 = '订金' then '订单'\n" +
                " when g.订单状态 is null or g.订单状态 = '退订C' then '退订单'\n" +
                " else '合同'  "+
                "  end as 订单类型,\n" +
                "  b.账目单号 登记单号,\n" +
                "  i.顾客姓名 客户姓名,\n" +
                "  i.电话号码 联系电话,\n" +
                "  case h.客户类型 when '公司' then '企业' else '个人' end 客户类型,\n" +
                "  d.身份证,\n" +
                "  b.发票项目,\n" +
                "  b.发票类型,\n" +
                "  b.账目日期 发票日期,\n" +
                "  b.账目金额 发票金额,\n" +
                "  b.交易凭证号 发票号,\n" +
                "  if(b.是否红冲='1','是','否') 是否红冲,\n" +
                "  (select a.账目单号 from 销售财务账目表 a where a.账目编号 = b.关联编号) 红冲发票登记单号,\n" +
                "  (select 账目日期 from `销售财务账目表` where 账目编号 = b.关联编号) 红冲日期,\n" +
                "  e.人员姓名 操作人,\n" +
                "  case b.同步状态  when '1' then '成功' when '2' then '失败' else '未传送' end 传送结果,\n" +
                "  b.同步日期 传送日期,\n" +
                "  b.备注,\n" +
                "  j.车架号,\n" +
                "  if(f.交车日期 is not null,'已交车','未交车') 交车状态,\n" +
                "  b.发票抬头,\n" +
                "  b.纳税人识别号,\n" +
                "  b.客户地址,\n" +
                "  b.客户开户行,\n" +
                "  b.客户账号,\n" +
                "  b.客户电话,\n" +
                "  b.操作日期 as 登记操作时间,\n" +
                "  case when b.是否红冲='1' then (select 操作日期 from 销售财务账目表 where 关联编号 = b.账目编号 and 账目类型 = '红冲发票') else '' end as 退票操作时间 \n" +
                " from 销售财务表 a \n" +
                " join `销售财务账目表` b on a.财务编号 =  b.财务编号\n" +
                " join 报价单表 h on a.订单编号 = h.初始报价单号\n" +
                " join \n" +
                " (\n" +
                "  select max(报价编号) 报价编号 from 报价单表 where 许可 = :permit group by 初始报价单号 \n" +
                "\n" +
                "  \n" +
                " )c on c.报价编号 = h.报价编号\n" +
                " left join 报价客户表 d on h.客户详细 = d.编号\n" +
                " left join 用户 e on b.修改用户 = e.用户名 \n" +
                " left join 报价交车表 f on h.交车 = f.编号\n" +
                " left join 报价订单表 g on h.订单编号 = g.订单编号\n" +
                " left join 顾客记录汇总 i on h.顾客编号 = i.顾客编号\n" +
                " left join lsh库存表 j on h.库存编号 = j.库存编号\n" +
                " where a.许可 = :permit\n" +
                " and b.账目类型 in(:typeList)\n" +
                " order by b.修改时间 desc,b.创建时间 desc\n" +
                ") a\n" +
                "where 1=1 ";
        Map params = new HashMap();
        params.put("permit",request.permit);
        params.put("typeList",typeList);
        // 查询条件
        if (!StringUtils.isNullOrBlank(request.orderType)) {
            sql += "and a.订单类型 = :orderType ";
            params.put("orderType",request.orderType);
        }
        if (!StringUtils.isNullOrBlank(request.vin)) {
            sql += "and a.车架号 like :vin ";
            params.put("vin","%" +request.vin + "%");
        }
        if (!StringUtils.isNullOrBlank(request.leaveCarState)) {
            sql += "and a.交车状态 = :leaveCarState ";
            params.put("leaveCarState",request.leaveCarState);
        }
        if (!StringUtils.isNullOrBlank(request.identityNumber)) {
            sql += "and a.身份证 like :identityNumber ";
            params.put("identityNumber","%" + request.identityNumber + "%");
        }
        if (!StringUtils.isNullOrBlank(request.orderNumber)) {
            sql += "and a.订单号 like :orderNumber ";
            params.put("orderNumber","%" +request.orderNumber + "%");
        }
        if (!StringUtils.isNullOrBlank(request.customerName)) {
            sql += "and a.客户姓名 like :customerName ";
            params.put("customerName","%" +request.customerName + "%");
        }
        if (!StringUtils.isNullOrBlank(request.customerPhone)) {
            sql += "and a.联系电话 like :customerPhone ";
            params.put("customerPhone","%" + request.customerPhone + "%");
        }
        if (!StringUtils.isNullOrBlank(request.invoiceItem)) {
            sql += "and a.发票项目 = :invoiceItem ";
            params.put("invoiceItem",request.invoiceItem);
        }
        if (!StringUtils.isNullOrBlank(request.registerNumber)) {
            sql += "and a.登记单号 like :registerNumber ";
            params.put("registerNumber","%" + request.registerNumber + "%");
        }
        if (!StringUtils.isNullOrBlank(request.invoiceNumber)) {
            sql += "and a.发票号 like :invoiceNumber ";
            params.put("invoiceNumber","%" + request.invoiceNumber + "%");
        }
        if (!StringUtils.isNullOrBlank(request.isRed)) {
            sql += "and a.是否红冲 = :isRed ";
            params.put("isRed",request.isRed);
        }
        if (!StringUtils.isNullOrBlank(request.invoiceType)) {
            sql += "and a.发票类型 = :invoiceType ";
            params.put("invoiceType",request.invoiceType);
        }
        return this.getPage(sql,params,request.getPageSize(),request.getPageIndex());
    }
}
